package com.changpei.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changpei.common.result.Result;
import com.changpei.core.pojo.Praise;
import com.changpei.core.service.PraiseService;
import com.changpei.core.utils.SearchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>Title : Praise</p>
 * <p>Description : praise的控制层</p>
 *
 * @author weibiao
 * @date 2020年3月7日
 */
@RestController
@RequestMapping("/praise")
@CrossOrigin
public class PraiseController {

    @Autowired
    private PraiseService praiseService;

    /**
     * <p>Title : add</p>
     * <p>Description : 新增Praise数据</p>
     *
     * @param praise
     * @return
     */
    @PostMapping(value = "/add")
    public Result<Long> add(@RequestBody Praise praise) {
        // 调用PraiseService实现添加Praise
        if (praiseService.save(praise)) {
            return Result.ok(praise.getId());
        }
        return Result.failed();
    }

    /**
     * <p>Title : delete</p>
     * <p>Description : Praise根据ID数据</p>
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<String> delete(@PathVariable Long id) {
        // 调用PraiseService实现根据主键删除
        if (praiseService.removeById(id)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : update</p>
     * <p>Description : Praise根据ID修改</p>
     *
     * @param praise
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<String> update(@RequestBody Praise praise, @PathVariable Long id) {
        // 设置主键值
        praise.setId(id);
        // 调用PraiseService实现修改Praise
        if (praiseService.updateById(praise)) {
            return Result.ok();
        }
        return Result.failed();
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : Praise分页搜索实现</p>
     *
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<Praise>> findPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用PraiseService实现分页查询Praise
        Page<Praise> pageInfo = praiseService.page(new Page<>(pageNum, pageSize));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findPage</p>
     * <p>Description : Praise分页条件搜索实现</p>
     *
     * @param praise
     * @param pageNum  : 当前页
     * @param pageSize : 每页显示多少条
     * @return
     */
    @PostMapping(value = "/search/{pageNum}/{pageSize}")
    public Result<Page<Praise>> findPage(@RequestBody(required = false) Praise praise, @PathVariable int pageNum, @PathVariable int pageSize) {
        // 调用PraiseService实现分页条件查询Praise
        Page<Praise> pageInfo = praiseService.page(new Page<>(pageNum, pageSize), SearchUtil.initQuery(praise));
        return Result.ok(pageInfo);
    }

    /**
     * <p>Title : findList</p>
     * <p>Description : 多条件搜索Praise数据</p>
     *
     * @param praise
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Praise>> findList(@RequestBody(required = false) Praise praise) {
        // 调用PraiseService实现条件查询Praise
        List<Praise> list = praiseService.list(SearchUtil.initQuery(praise));
        return Result.ok(list);
    }

    /**
     * <p>Title : findById</p>
     * <p>Description : 根据ID查询Praise数据</p>
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<Praise> findById(@PathVariable Long id) {
        // 调用PraiseService实现根据主键查询Praise
        Praise praise = praiseService.getById(id);
        return Result.ok(praise);
    }

    /**
     * <p>Title : findOne</p>
     * <p>Description : 多条件搜索Praise数据，结果返回一条</p>
     *
     * @param praise
     * @return
     */
    @PostMapping(value = "/findOne")
    public Result<Praise> findOne(@RequestBody(required = false) Praise praise) {
        // 调用PraiseService实现条件查询Praise
        Praise praiseResult = praiseService.getOne(SearchUtil.initQuery(praise));
        return Result.ok(praiseResult);
    }

    /**
     * <p>Title : findAll</p>
     * <p>Description : 查询Praise全部数据</p>
     *
     * @return
     */
    @GetMapping(value = "/all")
    public Result<List<Praise>> findAll() {
        // 调用PraiseService实现查询所有Praise
        List<Praise> list = praiseService.list();
        return Result.ok(list);
    }
}
